package com.eight.cloud.mybatisplus.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.*;
import com.eight.cloud.mybatisplus.handler.DynamicTableNameHandler;
import com.eight.cloud.mybatisplus.handler.MpDataPermissionHandler;
import com.eight.cloud.mybatisplus.handler.MpTenantHandler;
import com.eight.cloud.mybatisplus.method.injector.MpSqlInjector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;

/**
 * @ClassName: MybatisPlusConfig
 * @Author: TXC
 * @Date: 2024-10-05 22:15
 **/
@AutoConfigureAfter({SaasConfig.class})
@EnableTransactionManagement(proxyTargetClass = true)
public class MybatisPlusConfig {

    @Resource
    private SaasConfig saaSConfig;

    @Value("${spring.application.name}")
    private String module;
    /**
     * 添加分页插件
     */
    @Bean
    @ConditionalOnMissingBean(MybatisPlusInterceptor.class)
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 在 MyBatis-plus 配置数据权限插件
        interceptor.addInnerInterceptor(dataPermissionInterceptor());
        // 在 MyBatis-plus 配置租户插件
        interceptor.addInnerInterceptor(tenantLineInnerInterceptor());
        // 如果配置多个插件, 切记分页最后添加
        interceptor.addInnerInterceptor(paginationInnerInterceptor());
        // 在 MyBatis-plus 配置非法 SQL 拦截器
        //interceptor.addInnerInterceptor(illegalSQLInnerInterceptor());
        return interceptor;
    }

    @Bean
    public ISqlInjector sqlInjector() {
        return new MpSqlInjector();
    }

    /**
     * 分页插件，自动识别数据库类型
     */
    public PaginationInnerInterceptor paginationInnerInterceptor() {
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 设置数据库类型为mysql
        // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        paginationInnerInterceptor.setMaxLimit(-1L);
        return paginationInnerInterceptor;
    }


    /**
     * 数据权限插件
     */
    public DataPermissionInterceptor dataPermissionInterceptor() {
        return new DataPermissionInterceptor(new MpDataPermissionHandler(module));
    }


    /**
     * 多租户插件
     */
    public TenantLineInnerInterceptor tenantLineInnerInterceptor(){
        return new TenantLineInnerInterceptor(new MpTenantHandler(saaSConfig));
    }

    /**
     * 动态表名插件
     */
    public DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor() {
        return new DynamicTableNameInnerInterceptor(new DynamicTableNameHandler());
    }

    /**
     * 数据变更记录插件
     */
    public DataChangeRecorderInnerInterceptor dataChangeRecorderInnerInterceptor() {
        DataChangeRecorderInnerInterceptor dataChangeRecorderInnerInterceptor = new DataChangeRecorderInnerInterceptor();
        dataChangeRecorderInnerInterceptor.setBatchUpdateLimit(1000);
        return dataChangeRecorderInnerInterceptor;
    }

    /**
     * 非法SQL拦截插件
     */
    public IllegalSQLInnerInterceptor illegalSQLInnerInterceptor() {
        return new IllegalSQLInnerInterceptor();
    }
}
